<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <!-- disabled 按钮的禁用 boolean  !message.length 当输入框有值时 为false 不禁用 -->
    <van-button class="post-btn" @click="onPost" :disabled="!message.length">发布</van-button>
  </div>
</template>

<script>
import { addComment } from "@/api/comment";
export default {
  name: "CommentPost",
  data() {
    return {
      message: "",
    };
  },
  // 接受祖先的依赖注入
  inject: {
  articleId: {
      type: [Number, String, Object],
  	default: null
  } ,
},
  props: {
    userId: {
      type: [Object, Number, String],
      required: true,
    },
  },
  methods: {
    async onPost() {
      try {
        let res = await addComment({
          target: this.userId,
          content: this.message,
          art_id: this.articleId === this.target? this.articleId : null// 对文章评论无需文章id
        });
        console.log("发布成功");
        console.log(res);
        //   清空输入框
        this.message = "";
        // 将新评论的
        this.$emit("post-success", res.data.data);
      } catch (error) {
        this.$toast("评论失败!请重试!");
        console.log(error);
      }
    },
  },
};
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
